package com.rybbaby.tss.platform.tags;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.platform.initializer.SysDictionaryInitializer;

import com.rybbaby.tss.platform.sysDictionary.entity.SysDictionary;
import freemarker.core.Environment;
import freemarker.template.*;

/**
 * @author 谢进伟
 * @description 系统数据字典读取标签
 * @createDate 2016年11月29日 下午9:04:57
 */
public class SysDictionaryTag extends SimpleTagSupport implements TemplateDirectiveModel {

	/*************************************** 针对freemarker视图的标签实现 ***************************************************/

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		try {
			String groupCode = null, dictionaryCode = null;// 文件id字符串,多个id用第二个参数隔开
			Object paramValue = params.get("groupCode");
			if(paramValue instanceof TemplateScalarModel) {
				groupCode = ((TemplateScalarModel)paramValue).getAsString();
			}
			paramValue = params.get("dictionaryCode");
			if(paramValue instanceof TemplateScalarModel) {
				dictionaryCode = ((TemplateScalarModel)paramValue).getAsString();
			}
			if(StringUtils.isNotBlank(groupCode)) {// 根据分组码取字典项
				List<SysDictionary> list = SysDictionaryInitializer.getGroupMap().get(groupCode);
				if(list != null && !list.isEmpty() && StringUtils.isNotBlank(dictionaryCode)) {// 根据字典项编码取字典项
					String dicCode = dictionaryCode;
					Optional<SysDictionary> findFirst = list.parallelStream().filter((SysDictionary t)-> {
						 return t.getCode() != null && t.getCode().equals(dicCode);
					}).findFirst();
					if(findFirst.isPresent()) {
						SysDictionary sysDictionary = findFirst.get();
						if(body != null) {
							loopVars[0] = Configuration.getDefaultObjectWrapper(Configuration.getVersion()).wrap(sysDictionary);
							body.render(env.getOut());
						}
					}
				} else {
					if(body != null && StringUtils.isNotBlank(groupCode)) {
						loopVars[0] = Configuration.getDefaultObjectWrapper(Configuration.getVersion()).wrap(list);
						body.render(env.getOut());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*************************************** 针对jsp视图使的标签实现 ***************************************************/
	/**
	 * 资源值key
	 **/
	private String groupCode;
	/**
	 * 字典项编码
	 **/
	private String dictionaryCode;
	/**
	 * 文件输出的变量名字
	 **/
	private String var;

	@Override
	public void doTag() throws JspException, IOException {
		try {
			if(StringUtils.isNotBlank(groupCode)) {
				List<SysDictionary> list = SysDictionaryInitializer.getGroupMap().get(groupCode);
				if(list != null && !list.isEmpty()) {
					JspFragment body = this.getJspBody();
					if(body != null) {
						if(StringUtils.isNotBlank(dictionaryCode)) {// 根据字典项编码取字典项
							Optional<SysDictionary> findFirst = list.parallelStream().filter(new Predicate<SysDictionary>() {

								@Override
								public boolean test(SysDictionary t) {
									return t.getCode() != null && t.getCode().equals(dictionaryCode);
								}

							}).findFirst();
							if(findFirst.isPresent()) {
								SysDictionary sysDictionary = findFirst.get();
								this.getJspContext().setAttribute(var, sysDictionary, PageContext.PAGE_SCOPE);
								body.invoke(null);
								this.getJspContext().removeAttribute(var);// 移除页面的临时变量防止变量污染
							}
						} else {// 根据分组码取字典项
							for(SysDictionary sysDictionarys : list) {// 循环输出文件对象
								this.getJspContext().setAttribute(var, sysDictionarys, PageContext.PAGE_SCOPE);
								body.invoke(null);
							}
							this.getJspContext().removeAttribute(var);// 移除页面的临时变量防止变量污染
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.doTag();
	}

	public String getGroupCode() {
		return groupCode;
	}

	public void setGroupCode(String groupCode) {
		this.groupCode = groupCode;
	}

	public String getDictionaryCode() {
		return dictionaryCode;
	}

	public void setDictionaryCode(String dictionaryCode) {
		this.dictionaryCode = dictionaryCode;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

}
